package com.wang.service.impl;


import cn.hutool.db.sql.Order;
import com.aaa.entity.OOrder;
import com.aaa.entity.OPayLog;
import com.aaa.entity.UUserInfo;
import com.aaa.security.util.WebUtils;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;

import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wang.config.PayConfig;
import com.wang.mapper.OOrderMapper;
import com.wang.mapper.OPayLogMapper;
import com.wang.mapper.UUserInfoMapper;
import com.wang.service.PayService;
import com.wang.vo.PayInfoVO;
import com.wang.vo.PayStatusVo;
import com.wang.vo.PayUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.math.RoundingMode;
import java.util.*;


@Service("PayServiceImpl")
@Slf4j
public class PayServiceImpl implements PayService {
    @Value("${alipay.app-id}")
    private String appId;

    @Value("${alipay.private-key}")
    private String privateKey;

    @Value("${alipay.public-key}")
    private String publicKey;

    @Value("${alipay.gateway}")
    private String gateway;


    @Autowired
    private OOrderMapper oOrderMapper;
    @Autowired
    private OPayLogMapper oPayLogMapper;

    @Autowired
    private UUserInfoMapper uUserInfoMapper;



    @Override
    public Result getPayInfo(PayUtils payUtils) throws IOException {
        String shengcheng = PayConfig.shengcheng(payUtils);
        OOrder oOrder = new OOrder();
        oOrder.setAddrid( payUtils.getAddrid());
        UpdateWrapper<OOrder> oOrderUpdateWrapper = new UpdateWrapper<>();
        oOrderUpdateWrapper.eq("code",payUtils.getOrderno());
        oOrderMapper.update(oOrder,oOrderUpdateWrapper);
        Map<String,Object> map =new HashMap<>();
        map.put("codeUrl",shengcheng);
        map.put("code",payUtils.getOrderno());
        return new Result(2000,"sucess",map) ;
    }

    @Override
    public Result paystatus(String orderno) throws AlipayApiException {
        PayStatusVo zhuangtai = PayConfig.zhuangtai(orderno);
        if (zhuangtai!=null){


        if ("TRADE_SUCCESS".equals(zhuangtai.getTradeStatus())){
            oPayLogMapper.updatepaystatus(orderno,zhuangtai.getSendPayDate(),zhuangtai.getTradeStatus(),zhuangtai.getOutTradeNo());
            OOrder oOrder = new OOrder();
            oOrder.setState(0);
            UpdateWrapper<OOrder> oOrderUpdateWrapper = new UpdateWrapper<>();
            oOrderUpdateWrapper.eq("code",orderno);
            oOrderMapper.update(oOrder,oOrderUpdateWrapper);
            return new Result(2000,"支付成功",zhuangtai);
        }
        }
        return new Result(5000,"支付失败");
    }
}
